package com.zkw.group.controller;

import com.alibaba.fastjson.JSON;
import com.zkw.group.entity.*;
import com.zkw.group.service.PublicCourseService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;

@Api(description = "公众查看课程有关信息")
@CrossOrigin
@RestController
@RequestMapping("/Publics")
public class PublicCourseController {

    @Autowired
    private PublicCourseService publicCourseService;

    @ApiOperation(value = "分页查询所有课程信息")
    @GetMapping("/AllCourse")
    public String getAllPublicCourseList(QueryInfo queryInfo) {
        int numbers = publicCourseService.getPublicCourseCounts(queryInfo.getQuery());
        int pageStart = (queryInfo.getPageNum()-1)*queryInfo.getPageSize();
        List<Course> courses = publicCourseService.getAllPublicCourseList(queryInfo.getQuery(),pageStart,queryInfo.getPageSize());
        HashMap<String, Object> res = new HashMap<>();
        res.put("numbers",numbers);
        res.put("data",courses);
        return JSON.toJSONString(res);
    }

    @ApiOperation(value = "查询课程详细信息")
    @GetMapping("/CourseDetail")
    public String getCourseDetail(String courseid) {
        Course updateCourse = publicCourseService.getCourseDetail(courseid);
        String users_json = JSON.toJSONString(updateCourse);
        return users_json;
    }

    @ApiOperation(value = "分页查询指定课程章节信息")
    @GetMapping("/CoursePage")
    public String getAllPublicCoursePageList(QueryInfo queryInfo) {
        int numbers = publicCourseService.getPublicCoursePageCounts(queryInfo.getQuery(),queryInfo.getRole());
        int pageStart = (queryInfo.getPageNum()-1)*queryInfo.getPageSize();
        List<CoursePage> coursePages = publicCourseService.getAllPublicCoursePageList(queryInfo.getQuery(),queryInfo.getRole(),pageStart,queryInfo.getPageSize());
        HashMap<String, Object> res = new HashMap<>();
        res.put("numbers",numbers);
        res.put("data",coursePages);
        return JSON.toJSONString(res);
    }

    @ApiOperation(value = "分页查询课程先修后修关系列表")
    @GetMapping("/AllCourseRelation")
    public String getAllCourseRelationList(QueryInfo queryInfo) {
        int numbers = publicCourseService.getCourseRelationCounts(queryInfo.getQuery(),queryInfo.getRole());
        int pageStart = (queryInfo.getPageNum()-1)*queryInfo.getPageSize();
        List<CourseRelation> courseRelations = publicCourseService.getAllCourseRelationList(queryInfo.getQuery(),queryInfo.getRole(),pageStart,queryInfo.getPageSize());
        HashMap<String, Object> res = new HashMap<>();
        res.put("numbers",numbers);
        res.put("data",courseRelations);
        return JSON.toJSONString(res);
    }

    @ApiOperation(value = "分页查询课程有关参考书目信息")
    @GetMapping("/AllCourseBook")
    public String getAllCourseBookList(QueryInfo queryInfo) {
        int numbers = publicCourseService.getCourseBookCounts(queryInfo.getQuery(),queryInfo.getRole());
        int pageStart = (queryInfo.getPageNum()-1)*queryInfo.getPageSize();
        List<CourseBook> courseBooks = publicCourseService.getAllCourseBookList(queryInfo.getQuery(),queryInfo.getRole(),pageStart,queryInfo.getPageSize());
        HashMap<String, Object> res = new HashMap<>();
        res.put("numbers",numbers);
        res.put("data",courseBooks);
        return JSON.toJSONString(res);
    }
}
